package com.yc.blog.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.blog.biz.IBlogTypeBiz;
import com.yc.blog.biz.impl.BlogTypeBizImpl;

@WebServlet("/type")
public class BlogTypeController extends BasicController{
	
	private static final long serialVersionUID = -90302058705440480L;

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		
		if("add".equals(op)) {	
			add(request,response);
		}else if("finds".equals(op)) { //说明是第一次分页查询
			finds(request,response);
		}else if("findByPage".equals(op)) { //非第一次查询
			findByPage(request,response);
		}else if("update".equals(op)) { //跟新
			update(request,response);
		}else if("findAll".equals(op)) {
			findAll(request,response);
		}
	}

	
	private void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		IBlogTypeBiz typeBiz = new BlogTypeBizImpl();
		this.send(response, typeBiz.findAll());
	}


	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String tid = request.getParameter("tid");
		String status = request.getParameter("status");
		
		IBlogTypeBiz typeBiz = new BlogTypeBizImpl();
		this.send(response, typeBiz.update(tid, status));
	}

	private void findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		
		IBlogTypeBiz typeBiz = new BlogTypeBizImpl();
		this.send(response,typeBiz.findByPage(page, rows));;
	}

	private void finds(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		
		IBlogTypeBiz typeBiz = new BlogTypeBizImpl();
		this.send(response, typeBiz.findByFirst(page, rows));
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String tname = request.getParameter("tname");
		
		IBlogTypeBiz typeBiz = new BlogTypeBizImpl();
		this.send(response, typeBiz.add(tname));
	}
	
}
